// JavaScript Document
$( document ).ready(function() {
	try {
		$('.seitcm_button_wrapper *').button();
	} catch (e) {}
	try {
		$('.seitcm_button').button();
	} catch (e) {}
	try {
		$('.seitcm_button_set').buttonset();
	} catch (e) {}
	try {
		$('.seitcm_datepicker').datepicker({ dateFormat: 'dd/mm/yy' });
	} catch (e) {}
	try {
		$('.seitcm_datemypicker').datepicker({
			dateFormat: 'dd/mm/yy',
            changeMonth: true,
            changeYear: true,
            yearRange : 'c-65:c+10'
        });
	} catch (e) {}
	try {
		$('.seitcm_datemypostpicker').datepicker({
			dateFormat: 'dd/mm/yy',
            changeMonth: true,
            changeYear: true,
            yearRange : 'c-65:c+10',
            onSelect: function(date) {
            	this.form.submit();
            },
        });
	} catch (e) {}
	try {
		$('.seitcm_timepicker').timepicker();
	} catch (e) {}
	try {
		$('.seitcm_datetimepicker').datetimepicker();
	} catch (e) {}
	try {
		$('.seitcm_accordion').accordion();
	} catch (e) {}
	try {
		$( ".seitcm_sortable" ).sortable({});
	} catch (e) {}
	try {
		$( ".seitcm_sortable_column" ).sortable({
			connectWith: ".seitcm_sortable_column",
			handle: "h3",
			placeholder: "ui-state-highlight seitcm_widget_block_inner",
		});
		$( ".seitcm_widget_block_inner h3" ).click(function() {
			$( this ).next().slideToggle(300);
		});
	} catch (e) {}
	try {
	    $('.seitcm_numberic_texbox').keydown(function(event) {
	        // Allow: backspace, delete, tab, escape, and enter
	        if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 || 
	             // Allow: Ctrl+A
	            (event.keyCode == 65 && event.ctrlKey === true) || 
	             // Allow: home, end, up, down, left, right
	            (event.keyCode >= 35 && event.keyCode <= 40)) {
	                 // let it happen, don't do anything
	                 return;
	        }
	        else {
	            // Ensure that it is a number and stop the keypress
	            if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
	                event.preventDefault(); 
	            }   
	        }
    });
	} catch (e) {}
	try {
	    $('.seitcm_phone_texbox').keydown(function(event) {
	        // Allow: backspace, delete, tab, escape, and enter
	        if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 || 
	             // Allow: Ctrl+A
	            (event.keyCode == 65 && event.ctrlKey === true) ||
	            // Allow: Shift+= (sign +)
	            (event.keyCode == 187 && event.shiftKey === true) ||
	            (event.keyCode == 107) ||
	            // Allow: Shift+= (sign *)
	            (event.keyCode == 56 && event.shiftKey === true) ||
	            (event.keyCode == 107) ||
	            // Allow: Shift+= (sign #)
	            (event.keyCode == 51 && event.shiftKey === true) ||
	            // Allow: Shift+= (sign '(')
	            (event.keyCode == 57 && event.shiftKey === true) ||
	            // Allow: Shift+= (sign ')')
	            (event.keyCode == 48 && event.shiftKey === true) ||
	            // Allow: Spacebar
	            (event.keyCode == 32) ||
	             // Allow: home, end, up, down, left, right
	            (event.keyCode >= 35 && event.keyCode <= 40)) {
	                 // let it happen, don't do anything
	                 return;
	        }
	        else {
	            // Ensure that it is a number and stop the keypress
	            if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
	                event.preventDefault(); 
	            }   
	        }
    });
	} catch (e) {}
	try {
		$('.seitcm_form span').tooltip({
		    position: { my: "left+15 center", at: "right center" }
		});
		$('.seitcm_form input').tooltip({
		    disabled: true,
		    position: { my: "left+15 center", at: "right center" }
		}).on("focusin", function () {
		    $(this)
		        .tooltip("enable")
		        .tooltip("open");
		}).on("focusout", function () {
		    $(this)
		        .tooltip("close")
		        .tooltip("disable");
		});
	} catch (e) {}
	try {
		$('#seitcm_user_loginname').focus(function() {
			$('#seitcm_user_loginname').val(textfilter($('#seitcm_user_firstname').val()) + textfilter($('#seitcm_user_lastname').val()) + exportdm($('#seitcm_user_dob').val()));
		});
		$('#seitcm_user_firstname').keyup(function() {
			$('#seitcm_user_loginname').val(textfilter($('#seitcm_user_firstname').val()) + textfilter($('#seitcm_user_lastname').val()) + exportdm($('#seitcm_user_dob').val()));
		});
		$('#seitcm_user_lastname').keyup(function() {
			$('#seitcm_user_loginname').val(textfilter($('#seitcm_user_firstname').val()) + textfilter($('#seitcm_user_lastname').val()) + exportdm($('#seitcm_user_dob').val()));
		});
		$('#seitcm_user_dob').change(function() {
			$('#seitcm_user_loginname').val(textfilter($('#seitcm_user_firstname').val()) + textfilter($('#seitcm_user_lastname').val()) + exportdm($('#seitcm_user_dob').val()));
		});
		$('#seitcm_user_firstname').focusout(function() {
			$('#seitcm_user_loginname').val(textfilter($('#seitcm_user_firstname').val()) + textfilter($('#seitcm_user_lastname').val()) + exportdm($('#seitcm_user_dob').val()));
		});
		$('#seitcm_user_lastname').focusout(function() {
			$('#seitcm_user_loginname').val(textfilter($('#seitcm_user_firstname').val()) + textfilter($('#seitcm_user_lastname').val()) + exportdm($('#seitcm_user_dob').val()));
		});
		$('#seitcm_user_dob').focusout(function() {
			$('#seitcm_user_loginname').val(textfilter($('#seitcm_user_firstname').val()) + textfilter($('#seitcm_user_lastname').val()) + exportdm($('#seitcm_user_dob').val()));
		});
	} catch (e) {}
	
	try {			
		$('.seitcm_select_child_wrapper').find('.seitcm_select_child').removeAttr('name').removeAttr('required').hide().filter('.seitcm_select_child[title='+ $('.seitcm_select_parent').val() +']').show().attr('name', 'seitcm_select_child_post').prop("required", true);
	} catch (e) {}
	try {
		$('.seitcm_select_parent').on('change', function() {
			$('.seitcm_select_child_wrapper').find('.seitcm_select_child').removeAttr('name').removeAttr('required').hide().filter('.seitcm_select_child[title='+ $('.seitcm_select_parent').val() +']').show().attr('name', 'seitcm_select_child_post').prop("required", true);
		});
	} catch (e) {}
});

function textfilter(str) {
    str= str.toLowerCase();
    str= str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g,"a");
    str= str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g,"e");
    str= str.replace(/ì|í|ị|ỉ|ĩ/g,"i");
    str= str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g,"o");
    str= str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g,"u");
    str= str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g,"y");
    str= str.replace(/đ/g,"d");
    str= str.replace(/-|!|@|%|\^|\*|\(|\)|\+|\=|\<|\>|\?|\/|,|\.|\:|\;|\'| |\"|\&|\#|\[|\]|~|\$|\||_/g,"");
    return str;
    }
function exportdm(str) {
	if(str=='') return '';
    str= str.replace(/-|!|@|%|\^|\*|\(|\)|\+|\=|\<|\>|\?|,|\.|\:|\;|\'| |\"|\&|\#|\[|\]|~|\$|\||_/g,"/");
	var n=str.split("/");
    return n[0] + '' + n[1];
    }
